package com.ruoyi.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.purchase.dto.GenerateBoxCodeDTO;
import com.ruoyi.purchase.vo.BoxCodesVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.BoxCodes;
import com.ruoyi.purchase.service.IBoxCodesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 箱码列表Controller
 * 
 * @author ruoyi
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/purchase/boxCodes")
public class BoxCodesController extends BaseController
{
    @Autowired
    private IBoxCodesService boxCodesService;
    /**
     * 生成箱码号（测试用）
     */
    @GetMapping("/generateBoxCode")
    public AjaxResult generateBoxCode() {
        String boxCode = boxCodesService.generateBoxCode();
        return AjaxResult.success(boxCode);
    }

    /**
     * 生成批次号（测试用）
     */
    @GetMapping("/generateBatchCode")
    public String generateBatchCode() {
        String batchCode = boxCodesService.generateBatchCode();
//        System.out.println("批次号"+batchCode);
        return batchCode;
    }


    /**
     * 生成箱码
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:add')")
    @Log(title = "生成箱码", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generateBoxCodes(@RequestBody GenerateBoxCodeDTO generateBoxCodeDTO) {
        System.out.println("生成箱码页面"+generateBoxCodeDTO);
        return boxCodesService.generateAndSaveBoxCodes(generateBoxCodeDTO);
    }

    /**
     * 根据采购订单号查询未发货的箱码
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:list')")
    @GetMapping("/unshippedBoxCodes/{poId}")
    public TableDataInfo unshippedBoxCodes(@PathVariable("poId") String poId) {
        startPage();
        List<BoxCodesVO> list = boxCodesService.UnshippedBoxCodesByPoId(poId);
        System.out.println("箱码率先是"+list);
        return getDataTable(list);
    }



    /**
     * 查询箱码列表列表VO
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:list')")
    @GetMapping("/listVO")
    public TableDataInfo listVO(BoxCodes boxCodes) {
        startPage();
        System.out.println("箱码"+boxCodes);
        List<BoxCodesVO> list = boxCodesService.selectBoxCodesListVO(boxCodes);
        return getDataTable(list);
    }


    /**
     * 查询箱码列表列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:list')")
    @GetMapping("/list")
    public TableDataInfo list(BoxCodes boxCodes)
    {
        startPage();
        List<BoxCodes> list = boxCodesService.selectBoxCodesList(boxCodes);
        return getDataTable(list);
    }

    /**
     * 导出箱码列表列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:export')")
    @Log(title = "箱码列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoxCodes boxCodes)
    {
        List<BoxCodes> list = boxCodesService.selectBoxCodesList(boxCodes);
        ExcelUtil<BoxCodes> util = new ExcelUtil<BoxCodes>(BoxCodes.class);
        util.exportExcel(response, list, "箱码列表数据");
    }

    /**
     * 获取箱码列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        System.out.println("编号是是"+id);
        return success(boxCodesService.selectBoxCodesById(id));
    }

    /**
     * 新增箱码列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:add')")
    @Log(title = "箱码列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoxCodes boxCodes)
    {
        return toAjax(boxCodesService.insertBoxCodes(boxCodes));
    }

    /**
     * 修改箱码列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:edit')")
    @Log(title = "箱码列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoxCodes boxCodes)
    {
        return toAjax(boxCodesService.updateBoxCodes(boxCodes));
    }

    /**
     * 删除箱码列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:boxCodes:remove')")
    @Log(title = "箱码列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(boxCodesService.deleteBoxCodesByIds(ids));
    }
}
